package com.x.erp.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.model.Feedback;
import com.x.erp.rpc.FeedbackRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 用户反馈记录Controller
 * 
 * @author pansong
 */
@RestController
@RequestMapping(value = "/feedback")
public class FeedbackController extends BaseController {

	@Reference
	private FeedbackRpcService feedbackRpcService;// 用户反馈记录服务

	@Autowired
	private FastFileStorageClient fdfsClient;//文件服务 
	/**
	 * 查询用户反馈记录列表
	 * 
	 * @param feedback
	 * @return
	 */
	@RequestMapping(value = "/getFeedbackList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getFeedbackList(Feedback feedback) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", feedbackRpcService.selectFeedbackList(feedback));
	}

	/**
	 * 新增用户反馈记录
	 * 
	 * @param feedback
	 * @return
	 */
	@RequestMapping(value = "/insertFeedback", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertFeedback(Feedback feedback, MultipartFile file) {
		if (feedback == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户反馈记录不能为空！");
		}
		if (StringUtils.isBlank(feedback.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		if (StringUtils.isBlank(feedback.getFeedbackContent())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "反馈内容不能为空！");
		}
		String fdfsId = null;
		if(file != null && !file.isEmpty()){
			//上传图片
			try {
				fdfsId = fdfsClient.uploadFile(file.getInputStream(), "png");
				if(fdfsId == null){
					return new ResultModel(ResultCode.RESULT_FAIL, "图片上传服务器失败，请重新提交数据！");
				}
				feedback.setFeedbackImage(fdfsId);
			} catch (Exception e) {
//				e.printStackTrace();
				return new ResultModel(ResultCode.RESULT_FAIL, "图片上传服务器失败，请重新提交数据！");
			}
		}
		logger.info("fdfsId =======" +fdfsId);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", feedbackRpcService.insertFeedback(feedback));
	}

	/**
	 * 删除用户反馈记录
	 * 
	 * @param feedback
	 * @return
	 */
	@RequestMapping(value = "/deleteFeedback", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel deleteFeedback(Feedback feedback) {
		if (feedback == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户反馈记录不能为空！");
		}
		if (StringUtils.isBlank(feedback.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功", feedbackRpcService.deleteFeedback(feedback));
	}
	/**
	 * 查询用户反馈记录总数
	 * 
	 * @param feedback
	 * @return
	 */
	@RequestMapping(value = "/getFeedbackNum", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectFeedbackNum(Feedback feedback) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", feedbackRpcService.selectFeedbackNum(feedback));
	}
}
